<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
if(!check_admin("commission_LN")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "partenaires_admin";
	$filter = (!empty($_REQUEST['filter'])) ? $_REQUEST['filter'] : 0;
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$fields = describe('partenaires');
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	$value = (!empty($_REQUEST['value'])) ? $_REQUEST['value'] : 0;
	$message = "";
	$types = array();
	$q = "select id, nom from types_partenaire order by rank";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		$types[$s->id] = $s->nom;
	}
	$q = "select * from categories_partenaire";
	$r = ExecRequete($q);
	$categories = array();
	while($s = LigneSuivante($r)){
		$categories[$s->id] = $s;
	}


	if($action == 'form_export'){
		if(check_admin("commission_LN")){
			$categories = array();
			$q = "select * from categories_partenaire";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $categories[$s->id] = $s->description;
			$categories['tous'] = "<span class = 'bold'>TOUS</span>";
			echo "<form action = 'partenaires_admin.php' name = 'myform' method = 'post'>\n";
			echo "<h2>S&eacute;l&eacute;ctionner les cat&eacute;gories de partenaires</h2>";
			echo "<p class = 'center'><ul class = 'float'>\n";
			foreach($categories as $id => $print){
				$class = 'fade';
				echo "<li class = 'float' ><input type = 'checkbox' name = '$id' value = '1' onclick = 'if(this.checked) document.getElementById(\"cat$id\").className = \"bold\"; else document.getElementById(\"cat$id\").className = \"fade\"' /><span id = 'cat$id' class = '$class'>$print</span></li>\n";
			}
			echo "</ul></p>\n";
			echo "<br class = 'clear' />\n";
			echo "<h2>Choisir les champs &agrave; exporter</h2>\n";
			$fields = describe('partenaires');
			$fields['description'] = 'description';
			$fields['quantite'] = 'quantite';
			$fields['prix'] = 'prix';
			$fields['paye'] = 'paye';
			$fields['date_payement'] = 'date_payement';

			echo "<p class = 'center'><ul class = 'float'>\n";
			foreach($fields as $name => $type){
				if($name != "valide"){
					$checked = ($name == 'nom' || $name == 'prenom') ? "checked = 'checked'" : "";
					$class = ($checked) ? "bold" : "fade";
					echo "<li class = 'float'><input type = 'checkbox' name = '$name' value = '1' $checked  onclick = 'if(this.checked) document.getElementById(\"field$name\").className = \"bold\"; else document.getElementById(\"field$name\").className = \"fade\"' /><span id = 'field$name' class = '$class'>$name</span></li>\n";
				}
			}
			echo "</ul></p>\n";
			echo "<br class = 'clear' />\n";
			echo "<p class = 'center'><input type = 'checkbox' name = 'valide' value = '1' checked = 'checked' style = 'margin-right: 5px' /><span class = 'bold'>n'exporter que les partenaires valides</span><input type = 'hidden' name = 'action' value = 'export' /></p>\n";
			echo "<p class = 'center'><button type = 'submit' name = 'submit'><img src = 'images/icon_download.png' alt = '' width = '14' height = '14' />exporter</button>&nbsp;<button onclick = 'list_partenaires(1)'><img src = 'images/icon_list.png' alt = '' width = '14' height = '14' />retour &agrave; la liste</button></p>\n";
			echo "</form>";
		}
		else redirect('perso.php');
	}
	elseif($action == 'export'){
		if(!check_admin("commission_LN")) redirect('perso.php');
		else{
			require_once "include/php_writeexcel/class.writeexcel_workbook.inc.php";
			require_once "include/php_writeexcel/class.writeexcel_worksheet.inc.php";
			$fname = tempnam("tmp/","vbcll_export.xls");
			$workbook = new writeexcel_workbook($fname);
			$worksheet = $workbook->addworksheet();
			$heading = $workbook->addformat(array('align' => 'center', 'bold' => 1));
			$categories = array();
			$q = "select * from categories_partenaire";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)) $categories[$s->id] = $s->description;
			$categories['tous'] = "<span class = 'bold'>tous</span>";
			$fields = describe('partenaires');
			$selected = array();
			$print_fields = array();
			$valide = " valide = 1";
			foreach($categories as $id => $print){
				if(!empty($_REQUEST[$id])){ $selected[$id] = $id;}
			}
			$col = 0;
			$fields['description'] = 'description';
			$fields['quantite'] = 'quantite';
			$fields['prix'] = 'prix';
			$fields['paye'] = 'paye';
			$fields['date_payement'] = 'date_payement';
			foreach($fields as $field => $type){
				if($field == 'valide' && empty($_REQUEST['valide'])) $valide = "1=1";
				elseif(!empty($_REQUEST[$field])){
					$print_fields[] = $field;
					$worksheet->write(0, $col,  strtoupper($field),$heading);
					$col++;
				}
			}
			$join = ($valide == '1=1') ? "left" : "inner";
			$list_fields = implode(", ",$print_fields);
			if($list_fields){
				$export = str_replace(", ","\t",$list_fields).$endl;
				if(!empty($selected['tous'])){
					$q = "select id,$list_fields from partenaires $join soutiens on soutiens.partenaire_id = partenaires.id $join categories_partenaire on categories_partenaire.id = soutiens.categorie_id where $valide";
					$r = ExecRequete($q);
					$row = 1;
					while($s = mysql_fetch_row($r)){
						$id = array_shift($s);
						$col = 0;
						foreach($s as $data){
							$worksheet->write($row, $col, $data);
							$col++;
						}
						if($print_equipes){
							$membre = new membre($id);
							$worksheet->write($row, $col,  implode(", ",$membre->equipes));
						}
						$row++;
					}
				}
				else{
					$row = 1;
					foreach($selected as $cat){
						$q = "select id,$list_fields from partenaires inner join soutiens on soutiens.partenaire_id = partenaires.id inner categories_partenaire on categories_partenaire.id = soutiens.categorie_id where categories_partenaire.id = '$cat' and $valide ";
						$r = ExecRequete($q);
						while($s = mysql_fetch_row($r)){
							$id = array_shift($s);
							$col = 0;
							foreach($s as $data){
								$worksheet->write($row, $col, $data);
								$col++;
							}
							if($print_equipes){
								$membre = new membre($id);
								$worksheet->write($row, $col,  implode(", ",$membre->equipes));
							}
							$row++;
						}
					}
				}
				$workbook->close();
				header("Content-Type: application/x-msexcel; name=\"vbcll_partenaires_export.xls\"");
				header("Content-Disposition: attachment; filename=\"vbcll_partenaires_export.xls\"");
				$fh=fopen($fname, "rb");
				fpassthru($fh);
				unlink($fname);
			}
		}
	}


	elseif($action == 'list'){
		if(!empty($_SESSION['id'])) unset($_SESSION['id']);
		$table = new table('partenaires');
		$fields['actions'] = "type";
		foreach($fields as $field => $type){
			if($field == 'id' || $field == 'nom' || $field == 'localite' || $field == 'societe' || $field == 'actions' || $field == 'type_id'){
				$$field= new table_field($field,"partenaires");
				$$field->print = ($field != 'type_id') ? $field : 'type';
				$hidden = ($field == 'id') ? 1 : 0;
				$$field->hidden = $hidden;
				if($type == 'int(1)' || $field == 'actions') $$field->styleclass = 'center';
				$table->fields[] = $$field;

			}
		}
		$table->filter = 1;
		$actions->styleclass = 'center';
		if($filter) $join = "inner join soutiens on partenaires.id = soutiens.partenaire_id and soutiens.valide = 'Y'";
		$q = "select partenaires.id as id, partenaires.nom as nom, partenaires.localite as localite, partenaires.societe as societe from partenaires $join order by societe,nom,prenom";
		$r = ExecRequete($q);
		$i = 0;
		while($s = LigneSuivante($r)){
			$row = new table_row();
			$row->onclick = "onclick = 'edit_partenaire(\"$s->id\")'";
			foreach($fields as $field => $type){
				if($field == 'id' || $field == 'nom' || $field == 'localite' || $field == 'societe' || $field == 'actions' || $field == 'type_id'){
					if($field == 'actions'){
						$print =  "<img src = 'images/icon_edit.png' alt = 'edit' width = '14' height = '14' style = 'cursor: pointer' onclick = 'edit_partenaire($s->id)' />";
						$print .=  "<img src = 'images/icon_delete.png' alt = 'del' width = '14' height = '14' style = 'cursor: pointer' onclick = 'if(confirm(\"Voulez-vous vraiment supprimer le partenaire \\n$s->nom ?\"))del_partenaire($s->id)' />";
					}
					elseif($field == 'type_id') $print = $types[$s->$field];
					else{
						$print =  $s->$field;
					}
					$row->cols[$field] = $print;
				}
			}
			$table->rows[] = $row;
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'new_partenaire()' name = 'new'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouveau</button>&nbsp;<button type = 'button' onclick = 'export_partenaires()'><img src = 'images/icon_download.png' alt = '' width = '14' height = '14' />exporter</button><button type = 'button' onclick = 'ballons()'><img src = 'images/icon_ballon.png' alt = '' height = '14' width = '14' />ballons de matchs</button></p>\n";
		echo "<p class = 'small' style = 'text-align: right; font-size: 7pt; line-height: 7pt; padding-bottom: 0; margin-bottom: -15px;margin-right: 20px;'>";
		if($filter){
			echo "<a href = 'javascript: list_partenaires(0)'>Afficher toutes les entr&eacute;es</a>\n";
		}
		else{
			echo "<a href = 'javascript:list_partenaires(1)'>N'afficher que les entr&eacute;es valides</a>\n";
		}
		echo "</p>\n";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();

	}
	elseif($action == 'new'){
		$titre =  "Nouveau\n";
		$content =  "<form action = 'partenaires_admin.php' method = 'post' name = 'myform' onsubmit = 'return check(this)' enctype = 'multipart/form-data' >\n";
		foreach($fields as $field => $type){
			if($field != 'image' && $field != 'format') $content .= leftCol(str_replace('_'," ",$field),25);
			if($field == 'id'){
				$col = 'nouveau';
			}
			elseif($field == 'image'){
				$content .= "<div id = 'logo_partenaire' style = 'display: none; visibility: hidden'>";
				$content .= leftCol("logo",25);
				$col = "<input type = 'file' name = 'image' size = '10' />\n";
				$content .= rightCol($col,73);
				$content .= "</div>\n";
			}
			elseif($type == 'int(1)'){
				$col = "<input type = 'checkbox' checked = 'checked' name = '$field' value = '1' />";
			}
			elseif($field == 'url') $col = "http://<input type = 'text' size = '24' name = '$field' />\n";

			elseif($field == 'type_id'){
				$col = "<select name = 'type_id' >\n";
				$col .= "<option value = ''>-- veuillez choisir --</option>\n";
				foreach($types as $type_id => $nom_type){
					$col .= "<option value = '$type_id'>$nom_type</option>";
				}
				$col .= "</select>\n";
			}
			else{
				$col = "<input type = 'text' name = '$field' size = '30' />\n";
			}
			if($field != 'image') $content .= rightCol($col."<span id = 'error_$field'></span>",73);
		}
		$content .= "<br class = 'clear'>\n";
		$content .= "<div id = 'contrats'  style = 'border-top: 1px dotted #555; margin-bottom: 10px;padding-top: 0px' >\n";
		$content .= "</div>\n";

		$content .= "<p class = 'center'><input type = 'hidden' name = 'action' value = 'add' /><button type = 'submit' name = 'submit' value = 'add'>ajouter un partenaire</button>\n";
		$content .= "<button type = 'button' onclick = 'list_partenaires(1)'>annuler</button><input type = 'hidden' name = 'MAX_FILE_SIZE' value = '9999999' /></p>\n";
		$content .= "</form>\n";
		echo "<div class = 'center'>\n";
		box($titre,$content,650,1);
		echo "</div>\n";
	}
	elseif($action == 'edit'){
		$fichier = new partenaire($id);
		$_SESSION['id'] = $id;
		if($fichier->nom || $fichier->societe){
			$style = "style = 'display: inline;visibility: visible;'";
			echo "<div id = 'logo_partenaire' $style>\n";
			echo "<p class = 'center' style = ''>".($fichier->logo())."\n";
		#	echo "<span class = 'float_right'>\n";
			echo  "<div id='iframe' style = 'margin-left: 400px;'>\n";
			echo  "    <iframe src='logo_partenaire_upload.php?id=$fichier->id' frameborder='' scrolling='0' style = 'width: 300px'></iframe>\n";
			echo  "</div>\n";

		#	echo "</span>\n";
			echo "</p>";
			echo "</div>";
			$titre =  "Modifier $fichier->nom\n";
			$content =  "<form action = 'partenaires_admin.php' method = 'post' name = 'myform'  enctype = 'multipart/form-data' onsubmit = 'return check(this)' >\n";
			foreach($fields as $field => $type){
				if($field != 'image'){
					$content .= leftCol($field,25);
					if($field == 'id'){
						$col = "$fichier->id<input type = 'hidden' name= 'id' value = '$fichier->id' /><span id = 'error_$field'></span>";
					}
					elseif($field == 'url'){
						$print = str_replace("http://","",$fichier->$field);
						$col = "http://<input type = 'text' size = '24' name = '$field' value = '$print' /><span id = 'error_$field'></span>\n";
					}
					elseif($field == 'type_id'){
						$selected = (!$fichier->type) ? "selected = 'selected'" : "";
						$col = "<select name = '$field'><option value = '' disabled = 'disabled' $selected>- choisir -</option>\n";
						foreach($types as $type_id => $type){
							$selected = ($fichier->type_id == $type_id) ? "selected = 'selected'" : "";
							$col .= "<option value = '$type_id' $selected>$type</option>\n";
						}
						$col .= "</select>\n";
					}
					else{
						$col = "<input type = 'text' name = '$field' size = '30' value = '".$fichier->$field."' /><span id = 'error_$field'></span>\n";
					}
					$content .= rightCol($col,73);
				}
			}
			$content .= "<br class = 'clear'/>\n";
			$content .= "<div id = 'contrats'  style = 'border-top: 1px dotted #555; margin-bottom: 10px;padding-top: 0px' >\n";
			$content .= "</div>\n";
			$content .= "<p class = 'center'><input type = 'hidden' name = 'action' value = 'modif' /><button type = 'submit' name = 'submit' value = 'modif'>modifier</button>";
			$content .= "\n<button type = 'button' onclick = 'list_partenaires()'>annuler</button>\n<input type = 'hidden' name = 'MAX_FILE_SIZE' value = '9999999' />\n<input type = 'hidden' name = 'id' value = '$id' />\n";
			$content .= "<button type = 'button'  onclick = 'if(confirm(\"Voulez-vous vraiment supprimer le partenaire\\n$fichier->nom ?\"))del_partenaire($s->id)'>supprimer</button>\n";
			$content .= "\n</p>\n";
			$content .= "</form>\n";
			echo "<div class = 'center'>\n";
			box($titre,$content,650,1);
			echo "</div>\n";

		}
	}

	elseif($action == 'print_contrats'){
		$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
		echo "<p class = 'center bold'>Les contrats</p>\n";
		$table = new table('contrats');
		$fields = array(
			'soutiens.id' => 'id',
			'soutiens.quantite' => 'quantite',
			'categories_partenaire.description' => 'type',
			'categories_partenaire.id' => 'cat_id',
			'soutiens.prix' => 'prix',
			'soutiens.paye' => 'paye',
			'soutiens.date_payement' => 'date',
			'soutiens.valide' => 'valide'
		);
		$q = "select id, description from categories_partenaire order by type,id";
		$r = ExecRequete($q);
		$cats = array();
		while($s = LigneSuivante($r)) $cats[$s->id] = $s->description;
		$q = "select ";
		foreach($fields as $sql => $field){
			$q .= "$sql as $field, ";
			if($field == 'cat_id') continue;
			elseif($field == 'quantite') $$field->print = 'qtt';
			${"table_".$field} = new table_field($field,'contrats');
			if($field == 'id') ${"table_".$field}->hidden = 1;
			elseif($field != 'type' && $field != 'nom') ${"table_".$field}->styleclass = 'center';
			$table->fields[$field] = ${"table_".$field};

		}
		$field = new table_field('actions','contrats');
		$field->print = '';
		$field->styleclass = 'center';
		if($id) $table->fields['actions'] = $field;
		$fields['actions'] = 'actions';
		$q = substr($q,0,-2);
		$q .= " from categories_partenaire inner join soutiens on soutiens.categorie_id = categories_partenaire.id where soutiens.partenaire_id = $id";
		if($id){
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$row = new table_row();
				foreach($fields as $sql => $field){
					if($field == 'cat_id') continue;
					if($field == 'valide' || $field == 'paye'){
						$checked = ($s->$field == 'Y') ? "checked = 'checked'" : "";
						$row->cols[$field] = "<input type = 'checkbox' name = '".$s->id."_$field' value = 'Y' $checked />";
					}
					elseif($field == 'type'){
						error_log($s->cat_id);
						$row->cols[$field] = "<select name = '".$s->id."_cat_id'><option value = '' disabled = 'disabled'>-- choisir --</option>";
						foreach($cats as $cat_id => $cat_desc){
							$selected = ($cat_id == $s->cat_id) ? "selected = 'selected'" : "" ;
							$row->cols[$field] .= "<option value = '$cat_id' $selected>$cat_desc</option>";
						}
						$row->cols[$field] .= "</select>";
					}
					elseif($field == 'id') $row->cols[$field] = "<input type = 'hidden' name = '".$s->id."_id' value = '$s->id' />$s->id";
					elseif($field == 'quantite') $row->cols[$field] =  "<input type = 'text' size = '2' name = '".$s->id."_$field' value = '".$s->$field."' />";
					elseif($field == 'actions') $row->cols[$field] = "<button type = 'button' id = 'bouton_effaccer_$s->id' onclick = 'supprimer_contrat($s->id,$id)'>suppr.</button>";
					else $row->cols[$field] = "<input type = 'text' size = '5' name = '".$s->id."_$field' value = '".$s->$field."' />";
				}
				$table->rows[] = $row;
			}
		}
		$row = new table_row();
		foreach($fields as $sql => $field){
			if($field == 'cat_id') continue;
			if($field == 'valide' || $field == 'paye'){
				$checked = ($field == 'valide') ? "checked = 'checked'" : "";
				$row->cols[$field] = "<input type = 'checkbox' name = 'new_$field' id =  'new_$field' value = 'Y' $checked />";
			}
			elseif($field == 'type'){
				$row->cols[$field] = "<select name = 'new_cat_id' id = 'new_cat_id'><option value = '' disabled = 'disabled' selected = 'selected'>-- choisir --</option>";
				foreach($cats as $cat_id => $cat_desc){
					$row->cols[$field] .= "<option value = '$cat_id' $selected>$cat_desc</option>";
				}
				$row->cols[$field] .= "</select>";
			}
			elseif($field == 'date') $row->cols[$field] = date('d.m.Y');
			elseif($field == 'actions' && $id) $row->cols[$field] = "<button type = 'button' onclick = 'new_contrat($id)'>OK</button>";
			elseif($field == 'quantite') $row->cols[$field] = "<input type = 'text' size = '2' name = 'new_$field' id = 'new_$field' value = '".$s->$field."' />";
			elseif($field != 'actions') $row->cols[$field] = "<input type = 'text' size = '5' name = 'new_$field' id = 'new_$field' value = '".$s->$field."' />";
		}
		$table->rows[] = $row;

		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();

		exit;
	}

	elseif($action == 'add_contrat'){
		$fields = array(
			'cat_id' => 'cat_id',
			'quantite' => 'quantite',
			'prix' => 'prix',
			'paye' => 'paye',
			'valide' => 'valide',
		);
		foreach($fields as $field => $print) $$field = (isset($_REQUEST[$field])) ? $_REQUEST[$field] : "";
		if(!$cat_id) die("ERREUR: le type de soutien est inconnu");
		if(!$quantite) die("ERREUR: la quantite est vide");
		if(!$prix) die("ERREUR: le prix est vide");
		if(!$paye) $paye = 'N';
		if(!$valide) $valide = 'N';
		$q = "select count(*) as nb from soutiens where categorie_id = $cat_id and partenaire_id = $id";
		$r = ExecRequete($q);
		$s = LigneSuivante($r);
		if($s->nb > 0) die("Le soutien existe d&eacute;j&agrave;");
		$date = ($paye == 'Y') ? "curdate()" : "NULL";
		$q = "insert into soutiens (partenaire_id, categorie_id, quantite, prix, paye, date_payement, valide) values ($id, $cat_id, '$quantite', '$prix', '$paye',$date,'$valide')";
		echo (ExecRequete($q)) ? "Soutien ajout&eacute; avec succ&egrave;s" : "D&eacute;sol&eacute;, une erreur est survenue";
	}

	elseif($action == 'delete_contrat'){
		$q = "delete from soutiens where id = $id";
		echo (ExecRequete($q)) ? "contrat supprimer avec succ&egrave;s" : "d&eacute;sol&eacute;, une erreur est survenue";
		exit;
	}

	elseif($action == 'delete'){
		$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
		if($id){
			$fichier = new partenaire($id);
			if($fichier->delete()) echo "Le partenaire a &eacute;t&eacute; effac&eacute;";
			else echo "D&eacute;sol&eacute;, une erreur est survenue. Le partenaire n'a pas &eacute;t&eacute; effac&eacute;.";
			if(file_exists("images/partenaires/$id.jpeg")) unlink("images/partenaires/$id.jpeg");
		}
		else echo "D&eacute;sol&eacute;, une erreur est survenue. Le partenaire n'a pas &eacute;t&eacute; effac&eacute;.";
	}

	else{
		if($action == 'modif'){
			$partenaire = new partenaire($id);
			$fields = describe('partenaires');
			foreach($fields as $field => $type){
				if($field != 'image'){
					$partenaire->$field = $_REQUEST[$field];
				}
			}

			$fields = array(
				'soutiens.id' => 'id',
				'soutiens.quantite' => 'quantite',
				'soutiens.prix' => 'prix',
				'soutiens.paye' => 'paye',
				'soutiens.date_payement' => 'date',
				'soutiens.valide' => 'valide'
			);
			$q = "select * from soutiens where partenaire_id = $id";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				foreach($fields as $sql => $field) $$field = (isset($_REQUEST[$s->id."_".$field])) ? $_REQUEST[$s->id."_".$field] : "";
				if(!$paye) $paye = 'N';
				if(!$valide) $valide = 'N';
				$q = "update soutiens set ";
				foreach($fields as $sql => $field){
					if($field == 'id') continue;
					$q .= "$sql = '".$$field."', ";
				}
				$q = substr($q,0,-2);
				$q .= " where id = $s->id";
				ExecRequete($q);
			}

			// add new contrat
			$fields = array(
				'cat_id' => 'cat_id',
				'quantite' => 'quantite',
				'prix' => 'prix',
				'paye' => 'paye',
				'valide' => 'valide',
			);
			$insert = 1;
			foreach($fields as $field => $print) $$field = (isset($_REQUEST['new_'.$field])) ? $_REQUEST['new_'.$field] : "";
			if(!$cat_id) $insert = 0;
			if(!$quantite) $insert = 0;
			if(!$prix) $insert = 0;
			if(!$paye) $paye = 'N';
			if(!$valide) $valide = 'N';
			$q = "select count(*) as nb from soutiens where categorie_id = '$cat_id' and partenaire_id = $partenaire->id";
			$r = ExecRequete($q);
			$s = LigneSuivante($r);
			if($s->nb > 0) $insert = 0;
			if($insert){
				$date = ($paye == 'Y') ? "curdate()" : "NULL";
				$q = "insert into soutiens (partenaire_id, categorie_id, quantite, prix, paye, date_payement, valide) values ($id, $cat_id, '$quantite', '$prix', '$paye',$date,'$valide')";
				ExecRequete($q);
			}


			if($partenaire->update()) $message = "Modifications enregistr&eacute;es";
			else $message = "D&eacute;sol&eacute;, le partenaire n'a pas &eacute;t&eacute; modifi&eacute;";
		}
		elseif($action == 'add'){
			$types = array(
				1=>"gif",
				2=>"jpeg",
				3=>"png",
				4=>"swf",
				5=>"psd",
				6=>"bmp"
				);
			$dir = "images/partenaires";
			$partenaire = new partenaire();
			$fields = describe('partenaires');
			foreach($fields as $field => $type){
				if($field != 'image' && $field != 'id'){
					$partenaire->$field = $_REQUEST[$field];
				}
			}
			if(($partenaire->nom || $partenaire->societe) && $partenaire->type_id && (($partenaire->npa && $partenaire->localite) || $partenaire->email)){
				error_log("OK");
				$id = $partenaire->insert();
				$partenaire->id = $id;
				if(!empty($_FILES['image'])){
					$newname = $id.".jpeg";
					$filename = $_FILES['image']['name'];
					$type = $types[exif_imagetype($_FILES['image']['tmp_name'])];
					if(move_uploaded_file($_FILES['image']['tmp_name'], "images/partenaires/$filename")){
						list($width, $height) = getimagesize("$dir/".$filename);
						$dir = "images/partenaires";
						if($width < 180){
							rename("$dir/".$filename,"$dir/".$newname);
						}
						else{
							$new_width = 180;
							$new_height = 180 / $width * $height;
							$image_p = imagecreatetruecolor($new_width, $new_height);
							if($type == "jpeg"){
								$image = imagecreatefromjpeg("$dir/".$filename);
								imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
								imagejpeg($image_p,"$dir/".$newname, 180);
								unlink("$dir/".$filename);
							}
							elseif($type == "gif"){
								$image = imagecreatefromgif("$dir/".$filename);
								imagecopyresized($image_p, $image, 0, 0, $cropStartX, $cropStartY, $width, $height, $new_width, $new_height);
								imagejpeg($image_p,"$dir/".$newname, 180);
								unlink("$dir/".$filename);
							}
							elseif($type == "png"){
								$image = imagecreatefrompng("$dir/".$filename);
								imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
								imagejpeg($image_p,"$dir/".$newname, 180);
								unlink("$dir/".$filename);
							}
						}
						$filename = $newname;
						chmod($dir."/".$newname,0644);
						$partenaire->image = $newname;
						$partenaire->update();
					}
				}
			}
			else $id = 0;
			if($id){
				// add new contrat
				$fields = array(
					'cat_id' => 'cat_id',
					'quantite' => 'quantite',
					'prix' => 'prix',
					'paye' => 'paye',
					'valide' => 'valide',
				);
				$insert = 1;
				foreach($fields as $field => $print) $$field = (isset($_REQUEST['new_'.$field])) ? $_REQUEST['new_'.$field] : "";
				if(!$cat_id) $insert = 0;
				if(!$quantite) $insert = 0;
				if(!$prix) $insert = 0;
				if(!$paye) $paye = 'N';
				if(!$valide) $valide = 'N';
				$q = "select count(*) as nb from soutiens where categorie_id = '$cat_id' and partenaire_id = $id";
				$r = ExecRequete($q);
				$s = LigneSuivante($r);
				if($s->nb > 0) $insert = 0;
				if($insert){
					$date = ($paye == 'Y') ? "curdate()" : "NULL";
					$q = "insert into soutiens (partenaire_id, categorie_id, quantite, prix, paye, date_payement, valide) values ($id, $cat_id, '$quantite', '$prix', '$paye',$date,'$valide')";
					ExecRequete($q);
				}

				$message = "Partenaire ajout&eacute; avec succ&egrave;s";
			}
			else $message = "D&eacute;sol&eacute;, une erreur est survenue. Le partenaire n'a pas &eacute;t&eacute; ajout&eacute; !";

		}
		$script = "function check(){\n";
		$script .= "	if(!document.myform.nom.value && !document.myform.societe.value){\n";
		$script .= "		message(\"Vous n'avez pas fourni de nom !!\");\n";
		$script .= "		writediv('<span class = \"highlight bold\">&larr;</span>','error_nom');\n";
		$script .= "		writediv('<span class = \"highlight bold\">&larr;</span>','error_societe');\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	if((!document.myform.adresse.value || !document.myform.npa.value || !document.myform.localite.value) && ((document.myform.email.value.indexOf('.') <= 2) || (document.myform.email.value.indexOf('@') == 0))){\n";
		$script .= "		message('Veuillez fournir une adresse postale ou &eacute;lectronique');\n";
		$script .= "		writediv('<span class = \"highlight bold\">&larr;</span>','error_email');\n";
		$script .= "		writediv('<span class = \"highlight bold\">&larr;</span>','error_localite');\n";
		$script .= "		writediv('<span class = \"highlight bold\">&larr;</span>','error_npa');	\n";
		$script .= "		return false;		\n";
		$script .= "	}\n";
		$script .= "	return true;\n";
		$script .= "}\n";
		$script .= "function check_sponsor(text){\n";
		$script .= "	if(text == 'sponsor' || text == 'plaquette' || text == 'ballon'){\n";
		$script .= "		document.getElementById('logo_partenaire').style.visibility = 'visible';\n";
		$script .= "		document.getElementById('logo_partenaire').style.display = 'inline';\n";
		$script .= "	}\n";
		$script .= "	else{\n";
		$script .= "		document.getElementById('logo_partenaire').style.visibility = 'hidden';\n";
		$script .= "		document.getElementById('logo_partenaire').style.display = 'none';			\n";
		$script .= "	}\n";
		$script .= "}\n";

		entete("VBCLL partenaires",$script,"",1);
		if($message) message($message);
		echo "<h1>partenaires</h1>\n";
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_partenaires(1)</script>\n";
		pied();
	}
}

?>
